/**
 * 
 */
package metalink;

import vo.LoginInfoVo;

import model.ForgotPasswordMo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Sony
 * 
 */
public class ForgotPassword extends ActionSupport {
	String login_id="",question,reset="",site="",password1="";
	String security_question_answer="";
	int flag=0;
	//ArrayList<String> idquestion;
	
	
	public String execute() {
		if(login_id.equals(""))
		{
			flag=0;
			setFlag(flag);
			return SUCCESS;
		}
		else if(!login_id.equals("")&&!site.equals("")&&security_question_answer.equals("")&&password1.equals(""))
		{
			ForgotPasswordMo mo=new ForgotPasswordMo();
			try {
				//System.out.println("came here..");
				//ForgotPassword fp=new ForgotPassword();
				question=mo.getSecurityQuestion(login_id,site);
				flag=1;
				setFlag(flag);
				setLogin_id(login_id);
				setQuestion(question);
				setSite(site);
				return SUCCESS;
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//System.out.println("Sorry username was not found in our database");
				addActionError("Sorry.. Invalid user_id or site_id..");
				return ERROR;
				//e.printStackTrace();
			}
		}
		else if(!login_id.equals("")&&!site.equals("")&&!security_question_answer.equals("")&&password1.equals(""))
		{
			ForgotPasswordMo mo=new ForgotPasswordMo();
			LoginInfoVo vo=new LoginInfoVo();
			try {
				vo.setSecurity_question(question);
				vo.setLogin_id(login_id);
				vo.setSecurity_question_answer(security_question_answer);
				mo.checkSecurityAnswer(vo,site);
				flag=2;
				setFlag(flag);
				setLogin_id(login_id);
				setSite(site);
				//System.out.println("correct answer.."+login_id+site);
				
			} catch (Exception e) {
				addActionError("Wrong answer..");
				e.printStackTrace();
				return ERROR;
				
			}
		}
		else if(!login_id.equals("")&&!site.equals("")&&!password1.equals(""))
		{
			ForgotPasswordMo mo=new ForgotPasswordMo();
			LoginInfoVo vo=new LoginInfoVo();
			try {
				vo.setLogin_id(login_id);
				vo.setPassword(password1);
				mo.resetPassword(vo,site);
				flag=3;
				//System.out.println("password changed..");
				addActionError("password successfully changed.. Login to continue.");
				return "login";
				
			} catch (Exception e) {
				System.out.println("password mismatch..");
				e.printStackTrace();
				addActionError("oops.. something went wrong..");
				return ERROR;
			}
		}
		
		return ERROR;
	}


	public String getReset() {
		return reset;
	}


	public String getPassword1() {
		return password1;
	}


	public void setPassword1(String password1) {
		this.password1 = password1;
	}


	public String getSecurity_question_answer() {
		return security_question_answer;
	}


	public void setSecurity_question_answer(String security_question_answer) {
		this.security_question_answer = security_question_answer;
	}


	public void setReset(String reset) {
		this.reset = reset;
	}


	public String getSite() {
		return site;
	}


	public void setSite(String site) {
		this.site = site;
	}


	public String getLogin_id() {
		return login_id;
	}


	public void setLogin_id(String login_id) {
		this.login_id = login_id;
	}


	public String getQuestion() {
		return question;
	}


	public void setQuestion(String question) {
		this.question = question;
	}


	public int getFlag() {
		return flag;
	}


	public void setFlag(int flag) {
		this.flag = flag;
	}
}
